package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.wf.captcha.SpecCaptcha;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import utils.JwtUtil;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@RequiredArgsConstructor
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private StringRedisTemplate redisTemplate;
   // private  final UserInfoService userInfoService;
    private final SystemUserService systemUserService;
    @Override
    public CaptchaVo CaptchaVo() {
        //工具把文字生成图片
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        //生成随机的文字
        String lowerCase = specCaptcha.text().toLowerCase();
        //生成唯一的uuid
        UUID uuid = UUID.randomUUID();
        String key= RedisConstant.ADMIN_LOGIN_PREFIX +uuid;
        //返回给前端的信息
        CaptchaVo v=new CaptchaVo(specCaptcha.toBase64(),key);//第一个参数生成图片编码
        //把键值对保存到redis，没想到登录功能要用
        redisTemplate.opsForValue().set(key,lowerCase,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        return v;
    }

    @Override
    public String login(LoginVo loginVo) {
        if(loginVo!=null) {
            //判断redis里面有没有可用的验证码
            String s = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
            if(s==null)
            {
                throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED.getCode(),ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED.getMessage());
            }
            else {
                //判断
                if(!loginVo.getCaptchaKey().toLowerCase().equals(loginVo.getCaptchaKey()))
                    throw new RuntimeException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR.getMessage());
                else{
                    //校验用域名和验证码
                    loginVo.setPassword(DigestUtils.md5Hex(loginVo.getPassword()));
                    SystemUser one = systemUserService.lambdaQuery()
                            .eq(SystemUser::getPassword, loginVo.getPassword())
                            .eq(SystemUser::getUsername, loginVo.getUsername())
                            .one();
                    if(one!=null)
                    {
                        if(one.getStatus() == BaseStatus.DISABLE)
                        {
                            throw new LeaseException(308, "该用户已被禁用");
                        }
                        else return JwtUtil.createJwt(one.getId(), one.getUsername());
                    }
                    else  throw  new LeaseException(306,"账号不存在");
                }
            }

        }
        else throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND.getCode(),ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND.getMessage());
    }
}
